package com.springboot.demo.seria_deseria.util;

import com.sanbi.common.util.DateTimeUtil;

import com.springboot.demo.seria_deseria.core.CommonContext;
import com.springboot.demo.seria_deseria.core.CommonContextKeyEnum;
import com.springboot.demo.seria_deseria.core.CountryEnum;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * 国家工具类
 *
 * @author XiaoQingqing
 * @since 2024/01/05
 */
@Slf4j
public class CountryUtil {

    /**
     * 设置国家ID
     */
    public static void setCountryId(Integer countryId){
        CommonContext.set(CommonContextKeyEnum.COUNTRY_ID, countryId);
    }

    /**
     * 获取国家ID
     */
    public static Integer getCountryId(){
        return CommonContext.get(CommonContextKeyEnum.COUNTRY_ID);
    }

    /**
     * 获取国家ID
     */
    public static CountryEnum getCountryEnum(){
        return CountryEnum.getItem(getCountryId());
    }

    public static LocalDateTime convertDateTimeByTimeZone(LocalDateTime originDateTime){
        if(CountryUtil.getCountryEnum()!=CountryEnum.Mexico){
            ZoneId targetZoneId=CountryUtil.getCountryEnum().getZoneId();
            LocalDateTime convertedTime= DateTimeUtil.convertDateTimeByTimezone(originDateTime,CountryEnum.Mexico.getZoneId(), targetZoneId);
            return convertedTime;
        }else {
            return originDateTime;
        }
    }

    /**
     * 忽略国家
     */
    public static void ignoreCountry(){
        log.info("ignoreCountry");
        CommonContext.set(CommonContextKeyEnum.IGNORE_COUNTRY, true);
    }

    /**
     * 不忽略国家
     */
    public static void notIgnoreCountry(){
        log.info("notIgnoreCountry");
        CommonContext.set(CommonContextKeyEnum.IGNORE_COUNTRY, false);
    }

    /**
     * 是否忽略国家
     */
    public static boolean isIgnoreCountry(){
        return Boolean.TRUE.equals(CommonContext.get(CommonContextKeyEnum.IGNORE_COUNTRY));
    }

}
